package com.zhenai.rocyuan.controller;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhenai.rocyuan.service.mail.MailSendService;
import com.zhenai.rocyuan.service.user.UserService;
import com.zhenai.rocyuan.util.Pinyin4jUtil;
import com.zhenai.rocyuan.util.ServletUtils;


@Controller
@RequestMapping("/love")
public class ExampleController {
	   @Autowired
	    private UserService userService;
	   @Autowired
	    private MailSendService mailSendService;
	    
	 
	    
	    
	    /**
	     * 显示修改页面
	     * @param id
	     * @param model
	     * @return
	     */
	    @RequestMapping("/{loverName}")
	    public String get(@PathVariable("loverName") String loverName, Model model,HttpServletRequest request) {
	      
	    	String name = userService.getNameByPinyin(loverName);
	    
	    	String ip = ServletUtils.getRealIp(request);
	    	String email = "",email1="";
	    	if(Pinyin4jUtil.getPinYin(name).equals(name)){
	    		 model.addAttribute("loverName", loverName);	    		
	    	}else{
	    		
	    		if(name.length()==2){
	    			email1 = Pinyin4jUtil.getPinYin(name.charAt(1)+"")+"."+Pinyin4jUtil.getPinYin(name.charAt(0)+"");
	    			email = Pinyin4jUtil.getPinYin(name.charAt(0)+"")+"."+Pinyin4jUtil.getPinYin(name.charAt(1)+"");
	    		}else {
	    			email = Pinyin4jUtil.getPinYin(name.substring(1))+"."+Pinyin4jUtil.getPinYin(name.charAt(0)+"");
	    			
	    		}    		
	    	
	    		if(ip.startsWith("10.10")){
		    		email +="@zhenai.com";
		    		if(StringUtils.isNotBlank(email1)){
		    			email1+="@zhenai.com";
		    		}
	    		}else{
	    			email = "1045138965@qq.com";
	    		}
	    		
	    	}	    
	    	Map<String ,Object> root = new HashMap<String ,Object>();
	    	root.put("username", name);
	    	root.put("content", "来自IP为"+ip+"的朋友 为你点了一首双十一迟到的问候"+"http://rocxue.duapp.com/love/"+loverName+".html"+
	    	"<br/>曾经有一大票美女坐在我的身旁,但我没有好好珍惜,直到从三楼下来一大把技术部的挫男挤走了美女，才追悔莫及，如果上天再给我一次机会，<br/>我想对他们说三个字：哪来哪去");
	    	int rand = RandomUtils.nextInt(4)+1;
	    	boolean sendSuccess = false;
	    	if(StringUtils.isNotBlank(email)){
	    		sendSuccess = mailSendService.sendTemplateMail(root, email, "为了那已经逝去的单身情人节", "mail"+rand+".ftl");
	    	}
	    	
	    	if(StringUtils.isNotBlank(email1) && !sendSuccess){
	    		mailSendService.sendTemplateMail(root, email1, "为了那已经逝去的单身情人节", "mail"+rand+".ftl");
	    	}
	    	
	    	model.addAttribute("loverName", name);	
    		model.addAttribute("ip", ip);	
	        return "/example/lover11/index.jsp";
	    }
	    
	  
	
	    /*******************************    ftl  test                  ************************************/
	    @RequestMapping("/hello")
	    public String sayHello(ModelMap map){
	    	map.put("name", "hello ftl workld!");
	    	
			return "/hello.ftl";
	    	
	    }

}
